package edu.fjnu.fundsys.controller;

import java.awt.Dimension;
import java.util.ArrayList;
import java.util.List;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Date;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;

//import org.apache.commons.lang.StringUtils;


import edu.fjnu.fundsys.dao.ClientDao;
import edu.fjnu.fundsys.dao.ClientDaoJDBCImpl;
import edu.fjnu.fundsys.domain.Client;
import edu.fjnu.fundsys.service.ClientQueryHelper;
import edu.fjnu.fundsys.service.ClientService;
import edu.fjnu.fundsys.service.ClientServiceImpl;

                                 


public class ClientMgrServlet extends HttpServlet {

	/**
	 * Constructor of the object.
	 */
	public ClientMgrServlet() {
		super();
	}

	/**
	 * The doGet method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to get.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		
		request.setCharacterEncoding("utf-8");
		String act=request.getParameter("act");
		
		if("input".equals(act)){
			   request.getRequestDispatcher("jsps/client/input_client.jsp").forward(request, response);
			   
			}
		else if("create".equals(act)){
			Client client = new Client();
			client.setIdcard_no(request.getParameter("idcardno"));
			client.setClient_name(request.getParameter("clientname"));
			
			Boolean b = null;
			if(request.getParameter("clientsex").equals("true"))
				b=true;
			else if(request.getParameter("clientsex").equals("false"))
				b=false;
			client.setClient_sex(b);
			client.setClient_phone(request.getParameter("clientphone"));
			client.setClient_email(request.getParameter("clientemail"));
			client.setClient_address(request.getParameter("clientaddress"));
			client.setClient_hobby(request.getParameter("clienthobby"));
			client.setCreate_date(new Date());
			
			ClientService clientservice=new ClientServiceImpl();
			clientservice.createClient(client);

			response.sendRedirect("clientMgr?act=loadClient");
			}
		/*else if("loadClient".equals(act)){
			clientService clientservice=new clientServiceImpl();
			IClientDAO clientDAO = new ClientDAOImpl();
			request.setAttribute("clientList", clientservice.loadall());
			
			List<Client> list =clientservice.loadall();
			List account_list = new ArrayList();
			for(int i=0;i<list.size();i++){
				System.out.println(list.get(i).getClient_no());
				account_list.add(clientDAO.getClientAccount(list.get(i).getClient_no()));
				
			}
		
			//List client_list = (List) clientservice.loadall();
			//Dimension i=client_list.size();
			//System.out.println(i);
			request.setAttribute("clientAccount", account_list);
			request.setAttribute("allclientAccount", clientDAO.getAllClientAccount(1));
			
			request.getRequestDispatcher("jsps/client/normal_list_client.jsp").forward(request, response);
			
		}
		*/
		else if("loadClient".equals(act)){
			ClientService clientService=new ClientServiceImpl();
			request.setAttribute("clientList", clientService.loadall());
			
			request.getRequestDispatcher("jsps/client/normal_list_client.jsp").forward(request, response);
		}
		else if("loadClient_detail".equals(act)){
			ClientService clientservice=new ClientServiceImpl();
			request.setAttribute("clientList", clientservice.loadall());
			request.getRequestDispatcher("jsps/client/detail_list_client.jsp").forward(request, response);
		}
		else if("updateClient".equals(act)){
			int clientNo=Integer.parseInt(request.getParameter("clientno"));
			ClientService clientservice=new ClientServiceImpl();
			try {
				request.setAttribute("client",clientservice.getClientByNo(clientNo));
			} catch (Exception e) {
				e.printStackTrace();
			}
			request.getRequestDispatcher("jsps/client/update_client.jsp").forward(request, response);
		}
		else if("update".equals(act)){
			int clientNo=Integer.parseInt(request.getParameter("clientno"));
			ClientService clientservice=new ClientServiceImpl();
			
			try {
				Client c = clientservice.getClientByNo(clientNo);
				c.setClient_name(request.getParameter("clientname"));
				
				Boolean b = null;
				if(request.getParameter("clientsex").equals("true"))
					b=true;
				else if(request.getParameter("clientsex").equals("false"))
					b=false;
				c.setClient_sex(b);
				
				c.setClient_phone(request.getParameter("clientphone"));
				c.setClient_email(request.getParameter("clientemail"));
				c.setClient_address(request.getParameter("clientaddress"));
				c.setClient_hobby(request.getParameter("clienthobby"));
				//clientservice.updateObj(c);
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace(); 
			}
			
			response.sendRedirect("clientMgr?act=loadClient_detail");
		}
		
	}
		
	

	/**
	 * The doPost method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to post.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		doGet(request,response);
	}

}
